package nextoffer;

/**
 * @author ：szq
 * @date ：Created in 2022/3/17 19:32
 * @description：TreeNode
 * @modified By：
 * @version: $
 */
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
    TreeNode() {  }

    int i = 0 ;
    TreeNode preorderTraversal(int [] arr) //先序遍历  根  左  右
    {
        TreeNode root = new TreeNode(arr[i++]);//根节点5

        build( root,arr);
        return root;


    }

    void build(TreeNode root,int[] arr)
    {
        if(i== arr.length)return ;

        TreeNode left,right;
        if(i<arr.length)
        {

             left  = new TreeNode(arr[i++]);//根节点5
            root.left =left;
        }
        else return ;
      if(i<arr.length)
      {
            right  = new TreeNode(arr[i++]);//根节点5
          root.right = right;
      }
      else return ;


           // TreeNode right = new TreeNode(arr[i++]);//根节点5

        build(left,arr);
        build(right,arr);


            return  ;


    }

    public static void main(String[] args) {
        int[] ar=  new int[]{ 5,4,8,11,-1,13,4,7,2,-1,-1,5,1};
        TreeNode node = new TreeNode();
        node = node.preorderTraversal(ar);
        while(node!=null)
        {

            System.out.println(node.left.val);
            if(node.right!=null)
            System.out.println(node.right.val);

            node = node.left;

        }


    }
}
